﻿using Basic.UserCollect.Extend;
using Basic.UserModel.DB;
using Basic.UserModel.User;
using Basic.UserRemoteModel;
using Basic.UserRemoteModel.User.Model;
using Basic.UserRepository;
using WeDonekRpc.Helper;
using WeDonekRpc.Model;

namespace Basic.UserCollect.lmpl
{
    internal class UserCollect : BaseCollect<DBUserInfo, long>, IUserCollect
    {
        private readonly ILoginUserRepository _LoginUser;
        private readonly IUserRepository _User;
        public UserCollect (IUserRepository repository, ILoginUserRepository loginUser) : base(repository)
        {
            this._User = repository;
            this._LoginUser = loginUser;
        }
        public Result[] Query<Result> (UserQueryParam where, IBasicPage paging, out int count) where Result : class, new()
        {
            return this._User.Query<Result>(where, paging, out count);
        }
        public long RegUser (UserAdd add)
        {
            if (this._LoginUser.IsExist(c => c.LoginName == add.LoginName && c.LoginType == add.LoginType))
            {
                throw new ErrorException("user.login.name.repeat");
            }
            else if (this._User.IsExist(c => c.NickName == add.NickName))
            {
                throw new ErrorException("user.nick.name.repeat");
            }
            return this._User.RegUser(add);
        }
        public long GetUserId (string loginName, LoginType type)
        {
            return this._LoginUser.GetUserId(loginName, type);
        }
        public UserDatum UserLogin (long userId, string pwd)
        {
            UserDatum user = this._User.Get<UserDatum>(userId);
            if (user.Pwd != pwd)
            {
                throw new ErrorException("user.pwd.error");
            }
            else if (user.UserState != UserState.启用)
            {
                throw new ErrorException("user.already.disable");
            }
            RefreshLastLoginDate.Refresh(user);
            return user;
        }
        public UserDatum UserLogin (long userId)
        {
            UserDatum user = this._User.Get<UserDatum>(userId);
            if (user.UserState != UserState.启用)
            {
                throw new ErrorException("user.already.disable");
            }
            RefreshLastLoginDate.Refresh(user);
            return user;
        }

        public void SetPwd (long userId, string pwd)
        {
            this._User.SetPwd(userId, pwd);
        }

        public bool Disable (UserStateDto user)
        {
            if (user.UserState == UserState.已删除)
            {
                throw new ErrorException("user.already.delete");
            }
            else if (user.UserState == UserState.启用)
            {
                this._User.SetState(user.Id, UserState.禁用);
                return true;
            }
            return false;
        }

        public void Enable (UserStateDto user)
        {
            if (user.UserState == UserState.已删除)
            {
                throw new ErrorException("user.already.delete");
            }
            else if (user.UserState == UserState.禁用)
            {
                this._User.SetState(user.Id, UserState.启用);
            }
        }

        public bool Set (UserBase user, UserSet datum)
        {
            if (user.UserState != UserState.启用)
            {
                throw new ErrorException("user.already.disable");
            }
            else if (datum.IsEquals(user))
            {
                return false;
            }
            this._User.SetDatum(user.Id, datum);
            return true;
        }
    }
}
